﻿using System.Runtime.InteropServices;
using System.Speech.Synthesis;
using System.Text;

namespace ConsoleApp;

public class WindowsHelper
{
    // 导入 GetSystemInfo 函数 定义在 winbase.h 头文件中的，并且位于 kernel32.dll 动态链接库中
    [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
    static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo);
    
    // SYSTEM_INFO 结构体定义
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEM_INFO
    {
        public ushort wProcessorArchitecture;
        public ushort wReserved;
        public uint dwPageSize;
        public IntPtr lpMinimumApplicationAddress;
        public IntPtr lpMaximumApplicationAddress;
        public IntPtr dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public ushort wProcessorLevel;
        public ushort wProcessorRevision;
    };
    
    public void GetSystemInfoAndDisplay()
    {
        SYSTEM_INFO si = new SYSTEM_INFO();
        GetSystemInfo(out si);

        // 这里只是简单地输出一些信息到控制台，你可以根据需要进行修改
        Console.WriteLine("Number of Processors: " + si.dwNumberOfProcessors);
        Console.WriteLine("Processor Architecture: " + si.wProcessorArchitecture);
        // ... 其他你感兴趣的信息
    }
    
    // 导入 GetVolumeInformation 函数
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetVolumeInformation(
        string lpRootPathName, // 卷的根目录路径
        StringBuilder lpVolumeNameBuffer, // 接收卷名的缓冲区
        uint nVolumeNameSize, // 缓冲区大小
        out uint lpVolumeSerialNumber, // 接收卷序列号的变量
        out uint lpMaximumComponentLength, // 接收最大文件名组件长度的变量
        out FileSystemFeatureFlags lpFileSystemFlags, // 接收文件系统标志的变量
        StringBuilder lpFileSystemNameBuffer, // 接收文件系统名的缓冲区
        uint nFileSystemNameSize // 缓冲区大小
    );

    // 枚举类型以匹配Windows API中的FILE_SYSTEM_FLAGS_ALL常量
    [Flags]
    public enum FileSystemFeatureFlags : uint
    {
        // 根据需要添加相应的标志
        // 例如: FILE_CASE_SENSITIVE_SEARCH, FILE_CASE_PRESERVED_NAMES, ...
        // 注意：这些标志可能需要从其他Windows头文件或文档中获取
    }

    public void de()
    {

        // 例如，获取C:驱动器的信息
        string drive = "C:\\";

        // 为结果分配足够的缓冲区空间
        const int MAX_PATH = 260; // Windows中的最大路径长度
        var volumeName = new StringBuilder(MAX_PATH);
        var fileSystemName = new StringBuilder(MAX_PATH);

        uint volumeSerialNumber, maximumComponentLength;
        FileSystemFeatureFlags fileSystemFlags;

        // 调用GetVolumeInformation函数
        bool success = GetVolumeInformation(
            drive, volumeName, (uint)volumeName.Capacity,
            out volumeSerialNumber, out maximumComponentLength,
            out fileSystemFlags, fileSystemName, (uint)fileSystemName.Capacity);

        if (success)
        {
            Console.WriteLine("Volume Name: " + volumeName);
            Console.WriteLine("Volume Serial Number: " + volumeSerialNumber);
            Console.WriteLine("Maximum Component Length: " + maximumComponentLength);
            Console.WriteLine("File System Name: " + fileSystemName);
            // 根据需要处理fileSystemFlags
        }
        else
        {
            // 处理错误（例如，使用Marshal.GetLastWin32Error）
            Console.WriteLine("Failed to get volume information.");
        }
    }
    

}